import urllib.request
import urllib.parse#解析
#url='http://www.baidu.com'
url='http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%B1%DA%D6%BD&fr=ala&ala=1&pos=0&alatpl=wallpaper&oriquery=%E5%A3%81%E7%BA%B8'
response=urllib.request.urlopen(url=url)

#print(type(response))#<class 'http.client.HTTPResponse'>
#print(response.read())返回的一个二进制的值
#print(response.read().decode('utf-8'))
#print(response.readlines())
print(response.geturl())
print(response.getheaders())#响应头
print(response.getcode())#获取状态码
#urllib.request.urlretrieve(url=url,filename='壁纸.html')
img_url='http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98%E5%A3%81%E7%BA%B8&step_word=&hs=0&pn=15&spn=0&di=61490&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=337732323%2C1319358109&os=786657827%2C998612977&simid=4198069529%2C793516519&adpicid=0&lpn=0&ln=1043&fr=&fmq=1588562522262_R&fm=result&ic=0&s=undefined&hd=0&latest=0&copyright=0&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fblog.onlycj.com%2Fwp-content%2Fuploads%2F2016%2F11%2F1366.png&fromurl=ippr_z2C%24qAzdH3FAzdH3Fks52_z%26e3B5gsyv3_z%26e3Bv54AzdH3F%3Fr%3D8c9%266jrsyp5v54%3D8m&gsm=3&rpstart=0&rpnum=0&islist=&querylist=&force=undefined'
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
    }
request=urllib.request.Request(url=url,headers=headers)#请求对象定制
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
#urllib.request.urlretrieve(url=img_url,filename='壁纸.png')
#quote编码,会编码特殊符号
url='https://www.baidu.com/s?wd=韩红&sex=女'
s='福旺'
a=urllib.parse.quote(s)
print(a)
#多个参数编码
data={'wd':'韩红','sex':'女'}
data=urllib.parse.urlencode(data)
print(data)
url='https://www.baidu.com/s?'
url=url+data
print(url)